# IIS log archiver script.
#
# Copyright (c) 2009 - Jim Lehmer
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# 
# You may contact the author at jim [dot] lehmer [at] gmail [dot] com.
#
# You will need to change Powershell execution policy or sign this script to run it.
# The easiest is to open a Powershell window and run:
#
#   set-executionpolicy remotesigned
#
# However, that has security implications you should be aware of and research for yourself.
#
# Note: To keep from re-parsing logs over and over this script deletes IIS log files when
# it is finished (the currently open file can't be deleted because IIS has a lock on it).
# I usually run it right after midnight when our IIS server starts a new log file for the
# day. Anyway, there are other strategies you could use, such as moving the log files that
# have been parsed to another directory, etc.

$evt = new-object System.Diagnostics.EventLog("Application")
$evt.Source = "IISLogFileArchiver"
$infoevent = [System.Diagnostics.EventLogEntryType]::Information
$errevent = [System.Diagnostics.EventLogEntryType]::Error
$evt.WriteEntry("IISLogFileArchiver started", $infoevent)
new-psdrive -name Logs -psprovider FileSystem -root C:\WINDOWS\system32\LogFiles\W3SVC422211221 # You will need to change this to match your Web app's log file location.
set-location logs:
$logs = get-childitem * -exclude ("ex" + (get-date -uformat "%y%m%d") + ".log")

foreach ($log in $logs)
{
	# This was written to run on a 64-bit machine - change the path to the LogParser.exe if you are on 32-bit.
	# Change the database server name and database name to match your environment.
	& "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" ("SELECT * FROM " + $log.fullname + " TO LogTable") /o:SQL /server:DBSRVR /driver:"SQL Server" /database:IISLogs /createtable:OFF /clearTable:OFF

	if ($? -eq "True")
	{
		$evt.WriteEntry("IIS log " + $log.fullname + " archived successfully", $infoevent)
		remove-item $log

		if ($? -eq "True")
		{
			$evt.WriteEntry("IIS log " + $log.fullname + " deleted successfully", $infoevent)
		}
		else
		{
			$evt.WriteEntry("IIS log " + $log.fullname + " not deleted successfully", $errevent, $lastexitcode)
		}
	}
	else
	{
		$evt.WriteEntry("LogParser failed archiving IIS log", $errevent, $lastexitcode)
	}
}

$evt.WriteEntry("IISLogFileArchiver ended", $infoevent)